import { Address } from '../../utils/address.js';
import { Agent } from '../agent/agent-model.js';
import { My } from '../my/my-model.js';

var address = new Address();
var agent = new Agent();
var my = new My();

Page({
	data: {
		loadingHidden: false,
	},
	onLoad: function () {
		this._loadData();
	},
	onShow: function () {
		if (wx.getStorageSync('my_reload')){
			this._loadData();
			wx.setStorageSync('my_reload', false)
		}
		if (wx.getStorageSync('newOrder')) {
			this._loadData();
			wx.setStorageSync('newOrder', false)
		}
	},

	_loadData: function (callback) {
		var that = this;
		my.getUserInfo((data) => {
			that.setData({
				userInfo: data,
				loadingHidden: true
			});
		});
		my.getUser((data) => {
			that.setData({
				user: data
			})
			wx.setStorageSync('referral_code', data.referral_code);
		});
		callback&&callback();
	},

	/*修改或者添加地址信息*/
	editAddress: function () {
		var that = this;
		wx.getSetting({//先获取用户当前的设置  
			success(res) {
				if (!res.authSetting['scope.address']) {
					wx.authorize({
						scope: 'scope.address',
						success(res) {
							that.chooseWxAddress()
						},
						fail(res) {
							wx.showModal({
								title: '操作提示',
								content: '请授权使用微信通讯地址,点击确认开启授权',
								showCancel: true,
								success: function (res) {
									console.log(res)
									if (res.confirm == true) {
										wx.openSetting({
										})
									}
								}
							});
						}
					})
				} else {
					that.chooseWxAddress()
				}
			}
		})
	},

	chooseWxAddress: function () {
		wx.chooseAddress({
			success: function (res) {
				var addressInfo = {
					name: res.userName,
					mobile: res.telNumber,
					totalDetail: address.setAddressInfo(res)
				};
			}
		})
	},

	showMyOrder: function (event) {
		var status = my.getDataSet(event, 'id');
		wx.navigateTo({
			url: '../my-order/my-order?statusFromMy=' + status,
		})
	},

	toAgent:function(){
		var pass = false;
		agent.checkAgent((data)=>{
			pass = data.pass;
			if (pass) {
				wx.navigateTo({
					url: '../toagent/toagent?available_income=' + this.data.useravailable_income+'&phone='+this.data.phone,
				})
			} else {
				this.showTips('提示', '您的消费累计未到达' + this.data.user.agentLeastMoney + '元，暂时不能成为代理')
			}
		})
	},

	konwMore:function(){
		
	},
	agentMore:function(){
		wx.navigateTo({
			url: '../agent/agent?available_income=' + this.data.user.available_income + '&settled_income=' + this.data.user.settled_income+'&name='+this.data.user.name,
		})
	},

	callYanJiang: function () {
		wx.makePhoneCall({
			phoneNumber: '15700688882' //仅为示例，并非真实的电话号码
		})
	},

	/*下拉刷新页面*/
	onPullDownRefresh: function () {
		var that = this;
		this.data.orderArr = [];  //订单初始化
		this._loadData(() => {
			wx.stopPullDownRefresh();
		});
	},

    /*
     * 提示窗口
     * params:
     * title - {string}标题
     * content - {string}内容
     * flag - {bool}是否跳转到 "我的页面"
     */
	showTips: function (title, content) {
		wx.showModal({
			title: title,
			content: content,
			showCancel: false,
			success: function (res) {

			}
		});
	},
	//分享效果
	onShareAppMessage: function () {
		var referral_code = wx.getStorageSync('referral_code');
		return {
			title: '唐潮盐匠-自贡冷吃系列',
			path: 'pages/home/home?referral_code=' + referral_code
		}
	}

})